package com.dao;

import org.hibernate.Criteria;
import org.hibernate.Session;
import org.hibernate.criterion.Restrictions;

import com.hibernate.HibernateSessionFactory;
import com.model.Customer;



/**
 * 对Customer模型进行增删改查操作的类
 *  
 * @author 0912652
 */
public class CustomerDao {
	
	public Customer findCustomerByNum(String cnum){
		Session session = HibernateSessionFactory.getSession();
		Criteria criteria = session.createCriteria(Customer.class);
		
		criteria.add(Restrictions.eq("cnum", cnum));
		Customer customer = (Customer) criteria.uniqueResult();
		
		return customer;
	}
	
	public void saveCustomer(Customer customer){
		Session session = HibernateSessionFactory.getSession();
		try {
			session.beginTransaction();
			session.save(customer);
			session.getTransaction().commit();
		} catch (Exception e) {
			session.getTransaction().rollback();
			e.printStackTrace();
		} finally {
			HibernateSessionFactory.closeSession();
		}
		
	}

}
